﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class ColourBLL
  {
    public List<Colour> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Colours.OrderBy(p => p.Name).ToList();
      }
    }
    public List<Colour> GetByMode(int mode)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Colours.Where(p => p.Mode == mode).OrderBy(p => p.Name).ToList();  
      }
    }
    public Colour GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Colours.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public IQueryable GetByCateProductId(int CateProducttId)
    {
        var data = new DigitalColorCMSDataContext(_Global.StringConnect);
        var query = from p in data.ProdCates
                    join ps in data.Products on p.Id equals ps.CategoryId
                    join pc in data.ProductInColours on ps.Id equals pc.IdProduct
                    join c in data.Colours on pc.IdColour equals c.Id
                    select new
                    {
                        Name = c.Name,
                        Id = c.Id,
                        CateProductId = p.Id
                    };

        //query = query.Where(p => p.CateProductId == CateProducttId);
        int a = query.Count();
        return query;
    }
    public List<Colour> GetByCateProductId2(int CateProducttId)
    {

        ProdCateBLL dataCateProd = new ProdCateBLL();
        ProductBLL dataProduct = new ProductBLL();
        ProductInColourBLL dataProdInColor = new ProductInColourBLL();
        List<ProdCate> listProdCate = dataCateProd.GetByParent(CateProducttId);
        List<Product> listProduct = new List<Product>();
        List<ProductInColour> listProductInColor = new List<ProductInColour>();
        foreach (var item in listProdCate)
        {
            List<Product> obj = new List<Product>();
            obj = dataProduct.GetByCateId(item.Id);
            listProduct.AddRange(obj);
        }

        foreach (var item in listProduct)
        {
            List<ProductInColour> obj = new List<ProductInColour>();
            obj = dataProdInColor.GetByProductId(item.Id).ToList();
            if (obj != null)
                listProductInColor.AddRange(obj);
        }

        List<Colour> listColor = new List<Colour>();

        foreach (var item in listProductInColor)
        {
            Colour obj = GetById(item.IdColour);
            if (obj != null && listColor.Count > 0)
            {
                if (!CheckExits(obj.Id, listColor))
                {
                    listColor.Add(obj);
                }
            }
            else if (obj != null && listColor.Count == 0) listColor.Add(obj);
        }

        //listColor = listColor.GroupBy(p => p.Id, p => p.Name).ToList();
        return listColor;
    }

    private bool CheckExits(int id, List<Colour> lstColor)
    {
        foreach (Colour color in lstColor)
        {
            if (color.Id == id)
                return true;
        }
        return false;
    }

    public List<Colour> GetByProductId(string productId)
    {
      ProductInColourBLL data = new ProductInColourBLL();
      List<Colour> listOut = new List<Colour>();
      var list = data.GetByProductId(productId);
      foreach (var item in list)
      {
        Colour obj = GetById(item.IdColour);
        if (obj != null)
        {
          listOut.Add(obj);
        }
      }
      return listOut;
    }
    public void Add(Colour obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Colours.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }
    public void Update(Colour colour)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Colour obj = data.Colours.Where(p => p.Id == colour.Id).FirstOrDefault();
        if (obj != null)
        {
          obj.Name = colour.Name;
          obj.Name_En = colour.Name_En;
          obj.Mode = colour.Mode;
          obj.Image = colour.Image;
        }
        data.SubmitChanges();
      }
    }
    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Colour obj = data.Colours.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          data.Colours.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
    public void DeleteByMode(int mode)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Colour> obj = data.Colours.Where(p => p.Mode == mode).ToList();
        if (obj != null)
        {
          data.Colours.DeleteAllOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
  }
}
